package com.newenergy.wisehome.data.ormlite.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.newenergy.wisehome.common.helper.LogHelper;
import com.newenergy.wisehome.data.bean.FanLamp;
import com.newenergy.wisehome.data.ormlite.db.DatabaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 风扇灯数据库操作
 */

public class FanLampDao {
    private Context mContext;
    private Dao<FanLamp, Integer> fanLampDaoOpe;
    private DatabaseHelper helper;

    public FanLampDao(Context context) {
        mContext = context;
        try {
            helper = DatabaseHelper.getHelper(mContext);
            fanLampDaoOpe = helper.getDao(FanLamp.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param
     * @throws SQLException
     */
    public void add(FanLamp fanLamp) {
        try {
            fanLamp.setChecked(false);
            fanLampDaoOpe.create(fanLamp);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public int updateFan(FanLamp fanLamp) {
        try {
            fanLamp.setChecked(false);
            return fanLampDaoOpe.update(fanLamp);
            //    LogHelper.i("updateLightFannumber" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public FanLamp get(int id) {
        try {
            return fanLampDaoOpe.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public FanLamp getByUuid(String uuid) {
        List<FanLamp> fanLampList = null;
        try {
            fanLampList = fanLampDaoOpe.queryBuilder().where().eq
                    ("uuid", uuid).query();
            if (fanLampList != null && fanLampList.size() > 0) {
                return fanLampList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @return
     */
    public List<FanLamp> getAllFan() {
        List<FanLamp> fanLampList = new ArrayList<>();
        try {
            fanLampList = fanLampDaoOpe.queryBuilder().query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return fanLampList;
    }

    public FanLamp getById(String groupId,int category) {
        List<FanLamp> fanLampList = null;
        try {
            fanLampList = fanLampDaoOpe.queryBuilder().where().eq
                    ("groupId", groupId).and().eq
                    ("category", category).query();
            if (fanLampList != null && fanLampList.size() > 0) {
                return fanLampList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public List<FanLamp> getFamilyFanLamps(String familyId, int category) {
        List<FanLamp> fanLampList = new ArrayList<>();
        try {
            fanLampList = fanLampDaoOpe.queryBuilder().where().eq ("familyId", familyId).and().eq
                    ("category", category).query();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return fanLampList;
    }

    public int delete(FanLamp fanLamp) {
        try {
            return fanLampDaoOpe.delete(fanLamp);
            //    LogHelper.i("deleLightFannumber==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public void deleteAll() {
        try {
            int number = fanLampDaoOpe.deleteBuilder().delete();
            // LogHelper.i("deleteAll number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(List<FanLamp> fanLampList) {
        try {
            int number = fanLampDaoOpe.delete(fanLampList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void deleteFamilyFanLamps(String familyId) {
        try {
            List<FanLamp> fanLampList = getFamilyFanLamps(familyId,3);
            int number = fanLampDaoOpe.delete(fanLampList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public FanLamp getFanLampByName(String familyId, String name,int category) {
        try {
            List<FanLamp> fanLampList = fanLampDaoOpe.queryBuilder().where().eq
                    ("familyId", familyId).and().eq
                    ("name", name).and().eq
                    ("category", category).query();
            if (fanLampList != null && fanLampList.size() > 0)
                return fanLampList.get(0);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
